package com.moshang.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.moshang.blog.core.constant.CurrentUser;
import com.moshang.blog.entity.SysRole;
import com.moshang.blog.entity.SysUser;
import com.moshang.blog.entity.SysUserRole;
import com.moshang.blog.core.constant.MySysUser;
import com.moshang.blog.mapper.SysUsersMapper;
import com.moshang.blog.service.SysUserRoleService;
import com.moshang.blog.core.utils.ToolUtil;
import com.moshang.blog.service.SysUsersService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.*;


/**
 * @author moshang
 */
@Service("sysUsersService")
public class SysUsersServiceImpl extends ServiceImpl<SysUsersMapper, SysUser> implements SysUsersService {


    @Resource
    private SysUsersMapper sysUsersMapper;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Override
    public SysUser findUserByLoginName(String username) {
        Map<String,Object> map = Maps.newHashMap();
        map.put("username", username);
        return baseMapper.findUserByMap(map);
    }

    @Override
    public SysUser findUserById(Integer id) {
        Map<String,Object> map = Maps.newHashMap();
        map.put("id", id);
        SysUser sysUser=sysUsersMapper.findUserByMap(map);
        return sysUser;
    }

    @Override
    public Map selectUserMenuCount() {
      Map map=  baseMapper.selectUserMenuCount();
        return map;
    }

    @Override
    public int userCount(String param) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",param).or().eq("email",param);
        int count = baseMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public SysUser updateUser(SysUser user) {
        Integer count=0;
          user.setUpdateBy(MySysUser.id());
          user.setUpdateName(MySysUser.userName());
          user.setUpdateTime(new Date());
         count= baseMapper.updateById(user);
         if (count>0){
             CurrentUser currentUser= (CurrentUser) SecurityUtils.getSubject().getPrincipal();
             currentUser.setAge(user.getAge());
             currentUser.setRealName(user.getRealName());
         }
        //先解除用户跟角色的关系
        this.deleteUserRolesByUserId(user.getId());
        this.saveUserRoles(user.getId(),user.getRoleLists());
          return user;
    }

    @Override
    public SysUser findUserByThirdLoginId(Integer thirdId) {
        QueryWrapper<SysUser> param=new QueryWrapper<>();
      param.eq("third_login_id",thirdId);
        SysUser sysUser=baseMapper.selectOne(param);
        return sysUser;
    }

    @Override
    public void deleteUserRolesByUserId(Integer id) {
        baseMapper.deleteUserRolesByUserId(id);
    }

    @Override
    public void saveUserRoles(Integer id, Set<SysRole> roleLists) {
        baseMapper.saveUserRoles(id,roleLists);
    }

    @Override
    public SysUser updateUser(SysUser user, Integer[] roles) {
        Integer count = 0;
        user.setUpdateBy(MySysUser.id());
        user.setUpdateTime(new Date());
        count = baseMapper.updateById(user);
        if (count > 0) {
            CurrentUser currentUser = (CurrentUser) SecurityUtils.getSubject().getPrincipal();
            currentUser.setAge(user.getAge());
            currentUser.setRealName(user.getRealName());
        }
        //先解除用户跟角色的关系
        this.deleteUserRolesByUserId(user.getId());
        SysUserRole sysUserRole=new SysUserRole();
        sysUserRole.setUserId(user.getId());
        if (roles != null) {
            for (Integer role : roles) {
                sysUserRole.setRoleId(role);
                sysUserRoleService.save(sysUserRole);
            }
        }
        return user;
    }

    @Override
    public SysUser saveUser(SysUser user, Integer[] roles) {
        ToolUtil.entryptPassword(user);
        if (MySysUser.id()!=null){
          user.setCreateBy(MySysUser.id());
          user.setUpdateBy(MySysUser.id());
          user.setUpdateName(MySysUser.userName());
          user.setCreateName(MySysUser.userName());
        }
        user.setUpdateTime(new Date());
        user.setCreateTime(new Date());
        user.setIsActivited(0);
        baseMapper.insert(user);
        //保存用户角色关系
        SysUserRole sysUserRole=new SysUserRole();
        sysUserRole.setUserId(user.getId());
        if (roles != null) {
            for (Integer role : roles) {
                sysUserRole.setRoleId(role);
                sysUserRoleService.save(sysUserRole);
            }
        }
        return findUserById(user.getId());
    }

    @Override
    public void deleteUser(SysUser user) {
        //删除用户角色信息
        List<SysUserRole> list=sysUserRoleService.findByUserId(user.getId());
        List<Integer> idList = new ArrayList<>();
        for (SysUserRole userRole:list){
            idList.add(userRole.getId());
        }
        sysUserRoleService.deleteByIds(idList);
        //删除用户
        baseMapper.deleteById(user);
    }

    @Override
    public SysUser findByUserName(String userName) {
        QueryWrapper<SysUser> param=new QueryWrapper<>();
        param.eq("locked",0);
        param.eq("is_activited",0);
        param.eq("user_name",userName);
        SysUser sysUser=sysUsersMapper.selectOne(param);
        return sysUser;
    }



}
